1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans from sklearn.datasets import make_blobs
from IPython import display
%matplotlib inline
plt.rcParams['figure.figsize'] = [16, 9]
data, y = make_blobs(n_samples=1000, n_features=10, centers=10, random_state=42)
socres = [] for i in range(1, 12): kmeans = KMeans(i) model = kmeans.fit(data) socre = model.score(data) socres.append(abs(socre)) x_plot_data = list(range(1, 12)) plt.plot(x_plot_data, socres)
kmeans2 = KMeans(10) model2 = kmeans2.fit(data) labels = model2.predict(data)
fig = plt.figure() ax = Axes3D(fig) ax.scatter(data[:, 0], data[:, 1], data[:, 2], c=labels, cmap='tab10')
|